package com.software.levelupserver.controller;

import com.software.levelupserver.entity.User;
import com.software.levelupserver.exception.BizException;
import com.software.levelupserver.service.UserService;
import com.software.levelupserver.utils.ResultVOUtil;
import com.software.levelupserver.vo.Result;
import com.software.levelupserver.vo.UserCodeDTO;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/getCodeForRegister")
    public Result getRegisterCode(String email){
        if(StringUtils.isEmpty(email)){
            throw new BizException("邮箱为空");
        }

        userService.getCodeForRegister(email);
        return ResultVOUtil.success("发送成功！");
    }
    @GetMapping("/findAllUsers")
    public Result getAllUsers(@Min(1) Integer pageNum, @Min(1) Integer pageSize){

        return ResultVOUtil.success(userService.findAllUsers(pageNum,pageSize));
    }
    @GetMapping("/getCodeForUpdatePassword")
    public Result getRegisterCodeForUpdatePassword(String email){
        if(StringUtils.isEmpty(email)){
            throw new BizException("邮箱为空");
        }
        String result = userService.getCodeForUpdatePassword(email);
        Result success = ResultVOUtil.success(result);
        return success;
    }
    @PostMapping("/register")
    public Result register(@RequestBody @Valid UserCodeDTO userCodeDTO, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            throw new BizException("请填写完整信息！");
        }
        System.out.println(userCodeDTO.getUser());
        User user1 = userService.registerUser(userCodeDTO.getUser(), userCodeDTO.getCode());

        Result success = ResultVOUtil.success(user1);
        return success;
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody @Valid UserCodeDTO userCodeDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BizException("请填写完整信息！");
        }
        userService.updatePassword(userCodeDTO.getUser(),userCodeDTO.getCode());
        return ResultVOUtil.success("修改成功!");
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestPart User user, @RequestPart MultipartFile avatar) throws IOException {
        User user1 = userService.updateUser(user,avatar);
        return ResultVOUtil.success(user1);
    }

    /**
     * 更新用户活跃时间
     * @return
     */
    @PostMapping("/updateLoginTime")
    public Result updateLoginTime(){
        boolean b = userService.updateUserLoginTime();
        if (b){
            return ResultVOUtil.success("更新活跃时间成功");
        }else {
            return ResultVOUtil.error("更新活跃时间失败");
        }
    }

    @GetMapping("/getUserByUserId")
    public Result getUserByUserId(@NotNull(message = "userId为空") @Min(value = 1, message = "userId应大于1") Integer userId){
        User user = userService.findUserByUserId(userId);
        return ResultVOUtil.success(user);
    }

    /**
     * 获取本日活跃用户数量
     * @return
     */
    @GetMapping("/countUsersByLoginTime")
    public Result countUsersByLoginTime(){
        int activeNum = userService.countUsersByLoginTime();

        return ResultVOUtil.success(activeNum);
    }

    /**
     * 获取所有用户数量
     * @return
     */
    @GetMapping("/countUsers")
    public Result countUsers(){
        int userNum = userService.countUsers();

        return ResultVOUtil.success(userNum);
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }

    @GetMapping("/deleteUserById")
    public Result deleteUserById(Integer userId){
        return ResultVOUtil.success(userService.deleteUserById(userId));
    }
	/**
     * 更新用户经验值
     * @param experience
     * @return
     */
    @PostMapping("/updateExperience")
    public Result updateExperience(double experience){
        int isUp = userService.updateExperience(experience);
        if (isUp == -1){
            return ResultVOUtil.editResult("201", "未达到升级条件", null);
        } else if (isUp == 0) {
            return ResultVOUtil.error("更新经验值失败");
        } else {
            //更新用户等级
            User user = userService.updateGrade(isUp);
            if (user != null){
                return ResultVOUtil.success(user);
            }else {
                return ResultVOUtil.error("更新用户等级失败");
            }
        }
    }

    /**
     * 更新等级
     * @param gradeId
     * @return
     */
    @PostMapping("/updateGrade")
    public Result updateGrade(int gradeId){
        User user = userService.updateGrade(gradeId);
        if (user != null){
            return ResultVOUtil.success(user);
        }
        return ResultVOUtil.error("更新等级失败");
    }}
